//
//  EventManager.m
//  FootPrint
//
//  Created by alex tao on 4/2/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "EventManager.h"

@implementation EventManager

static EventManager * _sharedInstance = nil;

+ (EventManager*) sharedInstance
{
	if (_sharedInstance == nil) {
        _sharedInstance = [[EventManager alloc] init];
    }
	return _sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        _parser = [[EventListParser alloc] init];
        _parser.delegate = self;
        
        _eventList = [[NSMutableArray alloc] initWithCapacity:32];
        
        _beginDate = 99999999999999;
        _enddate = 0;
    }
    return self;
}

- (void)dealloc
{
    [_parser release];
    [_eventList release];
    [super dealloc];
}

- (void) updateLastList
{
    EventQueryResponseMessage_Summary * lastEvent = [_eventList lastObject];
    [_parser quaryEventByUser:[GConfig getUserId] beginDate:lastEvent.time endDate:0 andFilter:@"0000"];
}

- (void)updateAllList
{
    [self updateListOfBegin:0 endDate:99999999999999];
}

- (void) updateListOfBegin:(long long)beginDate endDate:(long long)endDate
{
    [_parser quaryEventByUser:[GConfig getUserId] beginDate:beginDate endDate:endDate andFilter:@"0000"];
}

- (void) mergeList:(PBArray*)newList beginDate:(long long)bgDate endDate:(long long)edDate
{
    if (newList.count > 0)
    {
        if (_beginDate > bgDate && _beginDate <= edDate) {
            _beginDate = bgDate;
        }
        if (_enddate < edDate && _enddate >= bgDate) {
            _enddate = edDate;
        }
        
        __block NSInteger beginIdx = -1;
        __block NSInteger endIdx = _eventList.count > 0 ? _eventList.count-1 : 0;

        [_eventList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            EventQueryResponseMessage_Summary * curEvent = (EventQueryResponseMessage_Summary*)obj;
            if (curEvent.time > bgDate && beginIdx == -1) {
                beginIdx = idx;
            }
            if (curEvent.time < edDate) {
                endIdx = idx;
            }
        }];
        
        if (beginIdx > endIdx || beginIdx == -1) {
            beginIdx = endIdx;
        }
        
        if (endIdx < _eventList.count) {
            [_eventList removeObjectsInRange:NSMakeRange(beginIdx, endIdx-beginIdx+1)];
        }
    
        for (int i = 0; i < newList.count; i++) {
            [_eventList insertObject:[newList objectAtIndex:i] atIndex:beginIdx+i];
        }
    }
}

- (NSArray*) eventListOfBegin:(long long)beginDate endDate:(long long)endDate
{
    if (endDate == 0) {
        endDate = 99999999999999;
    }
    
    NSMutableArray * retArr = [NSMutableArray arrayWithCapacity:4];
    for (EventQueryResponseMessage_Summary * msg in _eventList) {
        if (msg.time >= beginDate && msg.time <= endDate) {
            [retArr addObject:msg];
        }
    }
    return retArr;
}

- (void) removeAll
{
    [_eventList removeAllObjects];
}


#pragma mark - EventListDelegate

- (void) didRecieveEventList:(EventQueryResponseMessage*)event beginDate:(long long)bgDate endDate:(long long)edDate withFilter:(NSString*)filter
{
    if (event && event.resultCode == 0) {
        [self mergeList:event.eventSumList beginDate:bgDate endDate:edDate];
    }
}

@end
